
#include <wx/utils.h>

#include "Tux.h"
#include "MsnDef.h"
#include "MsnContact.h"
#include "PropertyWindow.h"

int ID_BUTTON_CLOSE = wxNewId();

int ID_PROPERTY_TEXTCTRL = wxNewId();

BEGIN_EVENT_TABLE(PropertyWindow, wxWindow)
	EVT_BUTTON(ID_BUTTON_CLOSE, PropertyWindow::OnClose)
	EVT_TEXT_URL(ID_PROPERTY_TEXTCTRL, PropertyWindow::OnUrlClick)
END_EVENT_TABLE()

PropertyWindow::PropertyWindow(wxWindow* parent, const wxPoint& position, MsnContact* contact)
	:wxDialog(parent,-1,wxEmptyString, wxDefaultPosition,wxDefaultSize,wxSIMPLE_BORDER)
{

	wxBitmap closeImage(_T("delete.gif"), wxBITMAP_TYPE_GIF);
	closeButton = new wxBitmapButton(this, ID_BUTTON_CLOSE, closeImage, wxDefaultPosition, wxDefaultSize);


	propertyText = new wxTextCtrl(this, wxID_ANY, _T(""),wxDefaultPosition,  wxDefaultSize,
                                wxNO_BORDER|wxTE_MULTILINE|wxTE_READONLY |wxTE_AUTO_URL);

	propertyText->Connect(wxEVT_COMMAND_TEXT_URL, wxTextUrlEventHandler(PropertyWindow::OnUrlClick), NULL, this); 

	//propertyText->Connect(wxEVT_RIGHT_UP, wxMouseEventHandler(PropertyWindow::OnRightClick), NULL, this); 

	
	//propertyText->SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOTEXT));
    propertyText->SetBackgroundColour(this->GetBackgroundColour());

	propertyText->SetBackgroundColour(*wxWHITE);
	this->SetBackgroundColour(*wxWHITE);

	propertyText->SetScrollbar(wxVERTICAL, 0, 0, 0);
//	propertyText->SetSize(200, 200);

	wxString text = contact->GetName();
	text<<_T(" (")<<MsnDef::GetStatusDesc(contact->GetStatus())<<_T(")\r\n");
	text<<contact->GetPassport()<<_T("\r\n");
	text<<contact->GetMesssage();

	propertyText->SetValue(text);


	wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL);

	topSizer->Add(closeButton, 0, wxALL|wxALIGN_RIGHT, 0);

	topSizer->Add(propertyText, 1, wxALL|wxEXPAND, 0);

	SetSizer(topSizer);

	int lines = propertyText->GetNumberOfLines();

	wxClientDC dc(this);
    dc.SetFont(propertyText->GetFont());
	int height = dc.GetTextExtent(propertyText->GetLineText(0)).y;

//	propertyText->SetSize(300, lines*height + 10);
	

	//this->SetPosition(wxGetMousePosition());
	this->SetPosition(position);

	this->SetSize(300, 300);

//	this->Fit();
	this->Show();

	//parent->Raise();
	//parent->SetFocus();
}

PropertyWindow::~PropertyWindow()
{
	//wxMessageBox(_T("propertyWindow"));
}

void PropertyWindow::OnClose(wxCommandEvent& event)
{
	//Destroy();
	Hide();
}


void PropertyWindow::OnUrlClick(wxTextUrlEvent& event)
{
	if(event.GetMouseEvent().LeftDown()){
		wxString url = propertyText->GetRange(event.GetURLStart(), event.GetURLEnd());
		DEBUG(_T("url click: ")+url);

		wxLaunchDefaultBrowser(url);
	}
}

void PropertyWindow::OnRightClick(wxMouseEvent& event)
{
	DEBUG(_T("right click"));
}

